<?php
namespace Home\Controller;

//---------------
//Author:Hua
//--------------

class CommentController extends CommonController {

    /*用户中心已评论商品首页*/
    public function index(){

        $comment = D('Comments');
        $goods = D('Goods');
        $detail = D('Detail');

        $com = $comment->where("userid = {$_SESSION['login']['id']}")->getAll();
        foreach($com as $key=>$val){
            $com[$key]['picname'] = $goods->where("id=".$val['goodsid'])->find()['picname'];
            $com[$key]['price'] = $goods->where("id=".$val['goodsid'])->find()['price'];
            
        }

        if(empty($com))$com[]['sg']=1;
        
        $this->assign('com',$com);
        $this->display();
    }

    /*商品评价页面*/
    public function comment(){
        $id = $_GET['id'];

    	$detail = D('Detail');
    	$map['orderid'] = $id;
    	$info = $detail->where($map)->select();

        $goods = D('Goods');
        foreach($info as $key=>$val){
            $info[$key]['picname'] = $goods->where("id=".$val['goodsid'])->find()['picname'];
        }
    	
    	$this->assign('info',$info);
        $this->assign('orderid',$id);
        $this->display();
    }

    /*对商品进行评论*/
    public function add(){
        //dump($_POST);

        $data['orderid'] = $_POST['orderid'];
        $data['goodsid'] = $_POST['goodsid'];
        $data['userid'] = $_SESSION['login']['id'];
        $data['content'] = $_POST['content'];
        $data['clevel'] = $_POST['clevel'];
        $data['ctime'] = time();

        $goods = D('Goods');
        $info = $goods->where("id={$_POST['goodsid']}")->find();
        $data['name'] = $info['goods'];

        
        $comment = D('Comments');
        if($comment->add($data)){
            $detail = D('Detail');
            $data2['orderid'] = $_POST['orderid'];
            $data2['goodsid'] = $_POST['goodsid'];
            $det = $detail->where($data2)->find();
            $data1['id'] = $det['id'];
            $data1['status'] = 1;
            $detail->save($data1);

            $this->redirect('Order/index');

        }
    }

    /*已评价商品详情*/
    public function detail(){
        //dump($_GET);
        $orderid = $_GET['id'];
        $comment = D('Comments');
        $goods = D('Goods');
        $info = $comment->where("orderid={$orderid}")->select();
        foreach($info as $key=>$val){
            $info[$key][picname] = $goods->where("id=".$val['goodsid'])->find()['picname'];

            $clevel = array('好评','中评','差评');
            $info[$key]['clevel'] = $clevel[$val['clevel']];
            date_default_timezone_set("PRC");
            $info[$key]['ctime'] = date('Y-m-d H:i:s',$info[$key]['ctime']);
            
        }
        //dump($info);

        $this->assign('orderid',$orderid);
        $this->assign('info',$info);
        $this->display();
    }


}
